﻿#region
using System;
using System.Data.Entity.Design.PluralizationServices;
using System.Globalization;
using System.IO;
using System.Windows.Forms;

#endregion

namespace MidnightPeach.EdmxTransformer
{
    public partial class EdmxTransformerForm : Form
    {
        public EdmxTransformerForm(string edmxPath, string folderForET)
        {
            InitializeComponent();

            _edmxPath = edmxPath;

            sfdET.InitialDirectory = folderForET;
            ofdET.InitialDirectory = folderForET;
        }

        private PluralizationService _pluralization;

        private readonly string _edmxPath;

        #region form event handlers
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (DesignMode)
                return;

            var presettables = this.GetChildren<IPresettable>();
            presettables.ForEach(x => x.Preset());

            _pluralization = PluralizationService.CreateService(new CultureInfo("en-US"));

#if DEBUG
#endif
        }
        #endregion

        #region menu item event handlers

        private void tsiOpen_Click(object sender, EventArgs e)
        {
            if (ofdET.ShowDialog() == DialogResult.OK)
            {
                _schema.Clear();
                _schema.ReadXml(ofdET.FileName);
            }
        }

        private void tsiSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(sfdET.FileName))
                tsiSaveAs.PerformClick();
            else
                _schema.WriteXml(sfdET.FileName);
        }

        private void tsiSaveAs_Click(object sender, EventArgs e)
        {
            if (sfdET.ShowDialog() == DialogResult.OK)
                _schema.WriteXml(sfdET.FileName);
        }

        private void tsiRefreshSchema_Click(object sender, EventArgs e)
        {
            if (ValidateEdmxPath() == false)
                return;

            EdmxSchema newSchema = EdmxHelper.ExtractSchema(_edmxPath);
            _schema.RefreshSchema(newSchema);
        }

        private void tsiApplyRules_Click(object sender, EventArgs e)
        {
            EdmxHelper.ApplyRules(_schema);
        }

        private void tsiTransformEdmx_Click(object sender, EventArgs e)
        {
            if (ValidateEdmxPath() == false)
                return;

            if (WinformUtility.Ask("Are you sure to transform target EDMX file? The file will be OVERWRITTEN") == false)
                return;

            EdmxHelper.Transform(_edmxPath, _schema);

            MessageBox.Show("EDMX has transformed");
        }
        #endregion

        #region grid event handlers
        private void dgvEntity_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex != 1 || e.RowIndex < 0)
                return;

            dgvEntity[2, e.RowIndex].Value = _pluralization.Pluralize(dgvEntity[1, e.RowIndex].Value.ToString());
        }
        #endregion

        #region methods
        private bool ValidateEdmxPath()
        {
            if (File.Exists(_edmxPath) == false)
            {
                MessageBox.Show("Invalid EDMX Path");
                return false;
            }
            else
                return true;
        }
        #endregion

        private void MainForm_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.PageUp)
                tbcTab.SelectedIndex = 0;
            else if (e.KeyCode == Keys.PageDown)
                tbcTab.SelectedIndex = 1;
        }
    }
}